import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ValidationPipe,
  Res,
  UseGuards,
} from '@nestjs/common';
import { Test2Service } from './test2.service';
import { CreateTest2Dto } from './dto/create-test2.dto';
import { UpdateTest2Dto } from './dto/update-test2.dto';
import { Test2 } from './entities/test2.entity';
import { Response } from 'express';
import { Roles } from '@/roles/roles.decorator';
import { RolesGuard } from '@/roles/roles.guard';

@Controller('test2')
// @UseGuards(RolesGuard)
export class Test2Controller {
  constructor(private readonly test2Service: Test2Service) {}

  @Post()
  // @Roles(['admin'])
  async create(
    @Body(new ValidationPipe({ whitelist: true }))
    createTest2Dto: CreateTest2Dto,
    @Res() res: Response,
  ) {
    res.status(200).send(await this.test2Service.create(createTest2Dto));
  }

  @Get()
  async findAll(): Promise<Test2[]> {
    return this.test2Service.findAll();
  }

  @Delete()
  removeList(@Body('ids') ids: number[]) {
    return this.test2Service.removeList(ids);
  }

  @Post('clear')
  clear(@Res() res: Response) {
    this.test2Service.clear();
    res.status(200).send({
      status: 0,
      message: '表清除成功!',
      data: [],
    });
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.test2Service.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateTest2Dto: UpdateTest2Dto) {
    return this.test2Service.update(+id, updateTest2Dto);
  }

  @Delete(':id')
  async remove(@Param('id') id: string) {
    const result = await this.test2Service.remove(+id);
    if (result.affected) {
      return { status: 0, message: '删除成功!', data: [] };
    } else {
      return { status: 1, message: '删除失败!', data: [] };
    }
  }
}
